package co.yixiang.hw.rest;

import co.yixiang.annotation.AnonymousAccess;
import co.yixiang.aop.log.Log;
import co.yixiang.hw.domain.YxStoreCoupon;
import co.yixiang.hw.domain.YxStoreCouponIssue;
import co.yixiang.hw.domain.YxStoreCouponIssueUser;
import co.yixiang.hw.domain.YxStoreCouponUser;
import co.yixiang.hw.service.*;
import co.yixiang.hw.service.dto.YxStoreCouponIssueDto;
import co.yixiang.hw.service.dto.YxStoreCouponIssueUserDto;
import co.yixiang.hw.service.dto.YxStoreCouponQueryCriteria;
import co.yixiang.hw.service.dto.YxUserDto;
import co.yixiang.utils.RedisUtils;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import org.apache.commons.lang3.StringUtils;
import org.springframework.data.domain.Pageable;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.util.Assert;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import io.swagger.annotations.*;
import java.io.IOException;
import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;

/**
* @author Walert
* @date 2020-03-10
*/
@Api(tags = "优惠券接口管理")
@RestController
@RequestMapping("/api/yxStoreCoupon")
public class YxStoreCouponController extends BaseController{

    private final YxStoreCouponService yxStoreCouponService;
    private final YxStoreCouponUserService couponUserService;
    private final YxStoreCouponIssueUserService couponIssueUserService;
    private final YxUserService userService;
    private final RedisUtils redisUtils;
    private final YxStoreCouponIssueService couponIssueService;



    public YxStoreCouponController(YxStoreCouponService yxStoreCouponService,
                                   YxStoreCouponUserService couponUserService,
                                   YxStoreCouponIssueUserService couponIssueUserService,
                                   YxUserService userService,
                                   RedisUtils redisUtils,
                                   YxStoreCouponIssueService couponIssueService) {
        this.yxStoreCouponService = yxStoreCouponService;
        this.couponUserService = couponUserService;
        this.couponIssueUserService = couponIssueUserService;
        this.userService = userService;
        this.redisUtils = redisUtils;
        this.couponIssueService = couponIssueService;

    }


    @GetMapping
    @Log("查询优惠券接口")
    @ApiOperation("查询优惠券接口")
    @AnonymousAccess
    public ResponseEntity<Object> getYxStoreCoupons(YxStoreCouponQueryCriteria criteria, Pageable pageable){
        if(criteria.getType() != null && criteria.getType() == 1){
            criteria.setUid(getUserId().intValue());
        }else{
            criteria.setUid(0);
        }
        return new ResponseEntity<>(yxStoreCouponService.queryAll(criteria,pageable),HttpStatus.OK);
    }



    @PostMapping
    @Log("用户领取优惠券接口")
    @ApiOperation("新增优惠券接口")
    @AnonymousAccess
    public ResponseEntity<Object> create(@RequestBody JSONObject params){
        Long userId = null;
        Integer issueCouponId = params.getInteger("issueCouponId");

        Integer count = (Integer)redisUtils.hget("coupon::issue-" + issueCouponId,"issue");
        if(count == null){
            YxStoreCouponIssueDto byId = couponIssueService.findById(issueCouponId);
            count = byId.getRemainCount();
        }
        Assert.isTrue(count > 0,"优惠券已领完");

        String phone = params.getString("phone");
        if(StringUtils.isNotBlank(phone)){
            YxUserDto yxUser = userService.checkUserAndRegister(params);
            if(yxUser != null){
                userId = yxUser.getUid();
            }else{
                Assert.isTrue(false,"获取用户信息失败");
            }
        }else{
            userId = getUserId();
        }
        params.put("uid",userId);
        Assert.isTrue(!couponIssueUserService.checkByUidAndIssueCouponId(userId.intValue(),issueCouponId),"您已领取该优惠券");

        Long addTime = System.currentTimeMillis()/1000L;
        YxStoreCouponIssueUser couponIssueUser = new YxStoreCouponIssueUser();
        couponIssueUser.setAddTime(addTime.intValue());
        couponIssueUser.setIssueCouponId(issueCouponId);
        couponIssueUser.setUid(userId.intValue());
        YxStoreCouponIssueUserDto dto = couponIssueUserService.create(couponIssueUser);
        couponUserService.saveCouponUser(params);

        JSONObject result = new JSONObject();
        count = (Integer)redisUtils.hget("coupon::issue-" + issueCouponId,"issue");
        result.put("count", count);
        result.put("issueUser",dto);

        return new ResponseEntity<>(result,HttpStatus.OK);
    }

    @PostMapping(value = "/userCoupon")
    @Log("获取用户优惠券接口")
    @ApiOperation("获取用户优惠券接口")
    public ResponseEntity<Object> getUserCoupon(@RequestBody JSONObject params){
        Long uid = getUserId();
        Integer goodsId = params.getInteger("goodsId");
        return new ResponseEntity<>(couponUserService.getUserCanUseCouponList(uid.intValue(),goodsId),HttpStatus.OK);
    }

    @PostMapping(value = "/v2/userCoupon")
    @Log("获取用户优惠券接口V2")
    @ApiOperation("获取用户优惠券接口V2")
    public ResponseEntity<Object> getUserCouponV2(@RequestBody JSONObject params){
        Long uid = getUserId();
        return new ResponseEntity<>(couponUserService.getUserCanUseCouponListV2(uid.intValue()),HttpStatus.OK);
    }

}